package com.example.myhotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.myhotel.entity.Bill;
import com.example.myhotel.mapper.BillMapper;
import com.example.myhotel.service.BillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;

@Service
@Slf4j
public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements BillService {
    @Autowired
    private BillService billService;

    public void addBill(String roomId, String userId) {
        Bill bill = new Bill();
        bill.setCustomerId(userId);
        bill.setRoomId(roomId);
        billService.save(bill);
    }

    @Override
    public void billUpdate(String id, int money) {
        log.error(String.valueOf(money));
        Bill bill = billService.getById(id);
        bill.setEndTime(LocalDateTime.now());
        long start = bill.getCreateTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        long end = bill.getEndTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        int minutes=(int)(end-start)/(1000*60)%60;// 如果超了20分钟就按足一小时算
        int hours = (int) (end-start)/(1000*60*60)%24;
        int count = 0;
        if (minutes>=20){
            count = hours+1;
        }else {
            count = hours;
        }
        log.error(String.valueOf(count*money));
        bill.setMoney(count*money);
        billService.updateById(bill);
    }
    //更改账单信息

}
